// player.lua
// Stores all functions related to the player

function CCSpawnSWEP( player, command, arguments )
	return
end
concommand.Add( "gm_giveswep", CCSpawnSWEP )

function GM:PlayerDisconnected( ply )
	local entlist = ents.FindByModel( "models/props_junk/Watermelon01.mdl" )
	for k, ent in pairs(entlist) do
		if ent.Founder == ply then
			ent.Remove(ent)
		end
	end
end

function CCSpawnSENT( player, command, arguments )
	return
end
concommand.Add( "gm_spawnsent", CCSpawnSENT )

function CCSpawnNPC( player, command, arguments )
	return
end
concommand.Add( "gmod_spawnnpc", CCSpawnNPC )

function CCSpawnVehicle( player, command, arguments )
	return
end
concommand.Add( "gm_spawnvehicle", CCSpawnVehicle )

function CCSpawn( player, command, arguments )
	return
end
concommand.Add( "gm_spawn", CCSpawn )

// Melon!
function ThinkMove()
	for n, ply in pairs(player.GetAll()) do
		if(ply:Team() == TEAM_MELON) then
			if(ply:KeyDown(8) || ply:KeyDown(16) || ply:KeyDown(512) || ply:KeyDown(1024) || ply:KeyDown(2)) then
				local totalyaw = ply:GetAngles().y
				local totalpitch = 0
				if(ply:KeyDown(8)) then
					// Well, we can just leave it the way it is.
				end
				if(ply:KeyDown(16)) then
					totalyaw = totalyaw - 180
				end
				if(ply:KeyDown(512)) then
					totalyaw = totalyaw + 90
				end
				if(ply:KeyDown(1024)) then
					totalyaw = totalyaw - 90
				end

				local melon = ply.melon
				if melon then
					if melon and melon:IsValid() then
						local vect = Angle(0, totalyaw, 0):Forward()
						local newvect = vect * 3000
						if melon:GetPhysicsObject() and melon:GetPhysicsObject():IsValid() then
							melon:GetPhysicsObject():ApplyForceCenter(newvect)
						end
					end
				end
			end
			if ply:KeyDown( IN_JUMP ) then
				local melon = ply.melon
				if melon.AIbrush and melon.AIbrush.enabled == 1 then
					if melon:GetPhysicsObject() and melon:GetPhysicsObject():IsValid() then
						melon:GetPhysicsObject():ApplyForceCenter( melon.AIbrush.dir * melon.AIbrush.power )
					end
				end
			end
		end
	end
end

// Melon br0kez?
function GM:PropBreak( breaker, prop )
	// Check if this is a melon.
	if(prop:GetClass() == "prop_physics" and prop:GetModel() == "models/props_junk/Watermelon01.mdl") then
		// Continue.
	else
		return GAMEMODE.BaseClass:PropBreak( breaker, prop ) // Let the base gamemode handle it.
	end
	
	if prop.Founder then
		prop.Founder:Kill()
	else
		// This is a melon without an owner.
		// Let the baseclass handle it.
		GAMEMODE.BaseClass:PropBreak( breaker, prop )
	end
end

// Melonize!
function Melonize(ply)
	// Create the melon
	local melon = ents.Create("prop_physics")
	melon:SetModel("models/props_junk/Watermelon01.mdl")
	melon:SetPos(ply:GetPos())
	melon:Spawn() 	
	melon:SetHealth(1000)
	melon.aHealth = 1000
	melon.Founder = ply
	melon.IsPly = true
	
	// Make the person spectate the melon.
	ply:Spectate(OBS_MODE_CHASE)
	ply:SpectateEntity(melon)
	ply:SetMoveType(MOVETYPE_OBSERVER)
	ply:StripWeapons()

	// Reference the entity
	ply.melon = melon
end

// Humanize!
function Humanize(ply)
	// Not that much to do here..
end

// No noclip, bi0tch!
function GM:PlayerNoClip(ply)
	return false
end

// Yuor wepunz!
function GM:PlayerLoadout(ply)
	if ply:Team() == TEAM_HUMAN then
		ply:Give("weapon_physcannon")
	end
	return false
end

// Select the teams!
function GM:PlayerInitialSpawn(ply)
	if TIMELEFT <= 675 then // Are we one-fourth through the game?
		ply:SetTeam(TEAM_MELON) // I deem you a melon!
	elseif TIMELEFT > 675 then
		ply:SetTeam(TEAM_HUMAN) // I deem you a human!
	end
end

// Player respawned!
function GM:PlayerSpawn(ply)
	GAMEMODE:SetPlayerSpeed(ply, 300, 600) 
	GAMEMODE.BaseClass:PlayerSpawn(ply)
	if ply:Team() == TEAM_HUMAN then
		ply:SetTeam(TEAM_HUMAN)
		Humanize(ply)
	elseif ply:Team() == TEAM_MELON then
		Melonize(ply)
		ply:SetTeam(TEAM_MELON)
	end
end

function GM:PlayerSelectSpawn(ply)
	if ply:Team() == TEAM_MELON then
		local spawns = ents.FindByClass("info_melon_start")
		if #spawns > 0 then
			local randomspawn = math.random(#spawns)
			return spawns[randomspawn]
		end
	end
	return GAMEMODE.BaseClass:PlayerSelectSpawn(ply)
end

// Player died!
function GM:PlayerDeath(victim, inflictor, killer)
	if victim:Team() == TEAM_HUMAN then
		victim:SetTeam(TEAM_MELON)
		GAMEMODE.BaseClass:PlayerDeath(victim, inflictor, killer)
	elseif victim:Team() == TEAM_MELON then
		local melon = victim.melon
		if melon and melon:IsValid() then
			local function killit(ent2kill)
				if ent2kill and ent2kill:IsValid() then
					ent2kill:Remove()
				end
			end
			melon:GibBreakClient(melon:GetPos())
			timer.Simple(5, killit, melon)
		end
		GAMEMODE.BaseClass:PlayerDeath(victim, inflictor, killer)
	end
end

/*--------------------------------------------------------- 
    Name: gamemode:PlayerSelectSpawn( player ) 
    Desc: Find a spawn point entity for this player 
 ---------------------------------------------------------*/ 
 function GM:PlayerSelectSpawn( pl ) 
   
 	// Save information about all of the spawn points 
 	// in a team based game you'd split up the spawns 
 	if (self.SpawnPoints == nil) then
 	
 		self.LastSpawnPoint = 0 
 		self.SpawnPoints = ents.FindByClass( "info_player_start" ) 
 		self.SpawnPoints = table.Add( self.SpawnPoints, ents.FindByClass( "info_player_deathmatch" ) ) 
 		self.SpawnPoints = table.Add( self.SpawnPoints, ents.FindByClass( "info_player_combine" ) ) 
 		self.SpawnPoints = table.Add( self.SpawnPoints, ents.FindByClass( "info_player_rebel" ) ) 
 		 
 		// CS Maps 
 		self.SpawnPoints = table.Add( self.SpawnPoints, ents.FindByClass( "info_player_counterterrorist" ) ) 
 		self.SpawnPoints = table.Add( self.SpawnPoints, ents.FindByClass( "info_player_terrorist" ) ) 
 		 
 		// DOD Maps 
 		self.SpawnPoints = table.Add( self.SpawnPoints, ents.FindByClass( "info_player_axis" ) ) 
 		self.SpawnPoints = table.Add( self.SpawnPoints, ents.FindByClass( "info_player_allies" ) ) 
   
 		// (Old) GMod Maps 
 		self.SpawnPoints = table.Add( self.SpawnPoints, ents.FindByClass( "gmod_player_start" ) ) 
 		 
 		// If any of the spawnpoints have a MASTER flag then only use that one. 
 		 
 		for k, v in pairs( self.SpawnPoints ) do 
 		 
 			if ( v:HasSpawnFlags( 1 ) ) then 
 			 
 				self.SpawnPoints = {} 
 				self.SpawnPoints[1] = v 
 			 
 			end 
 		 
 		end 
   
 	end 
 	 
 	local Count = table.Count( self.SpawnPoints ) 
 	 
 	if ( Count == 0 ) then 
 		Msg("[PlayerSelectSpawn] Error! No spawn points!\n") 
 		return nil  
 	end 
 	 
 	local ChosenSpawnPoint = nil 
 	
 	// Try to work out the best, random spawnpoint (in 6 goes) 
 	for i=0, 6 do
 	 
 		ChosenSpawnPoint = self.SpawnPoints[ math.random( 1, Count ) ] 
 		 
 		if ( ChosenSpawnPoint && 
 			ChosenSpawnPoint:IsValid() &&
 			ChosenSpawnPoint:IsInWorld() &&
 			ChosenSpawnPoint != pl.LastSpawnpoint &&
 			ChosenSpawnPoint != self.LastSpawnPoint ) then
 			 
 			self.LastSpawnPoint = ChosenSpawnPoint 
 			pl.LastSpawnpoint = ChosenSpawnPoint
 			return ChosenSpawnPoint 
 			 
 		end
 	
 	end
 	 
 	return ChosenSpawnPoint
 	 
 end 